package com.tanhua.dubbo.api.db;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.*;
import com.tanhua.dubbo.mapper.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Author Jyz
 * @Date 2021/8/23 - 13:34
 * @Description com.tanhua.dubbo.api.mongo
 * @Since version-11
 */
@Service//灵魂测试APi实现类
public class TestSoulApiImpl implements TestSoulApi {

    @Autowired
    private SoultestPaperMapper soultestPaperMapper;    //操作试卷表通用mapper
    @Autowired
    private SoultestOptionMapper soultestOptionMapper;   //操作选项表通用mapper
    @Autowired
    private SoultestQuestionMapper soultestQuestionMapper;  //问题表通用mapper
    @Autowired
    private SoultestResultMapper soultestResultMapper;      //结果表通用mapper
    @Autowired
    private SoultestConclusionMapper soultestConclusionMapper;      //鉴定结果表通用mapper
    @Autowired
    private UserInfoMapper userInfoMapper;      //用户信息表通用mapper

    /**
     * 查询到所有的试卷对象
     * @return
     */
    @Override
    public List<SoultestPaper> getPaper() {
        return soultestPaperMapper.selectList(null);
    }


    /**
     * 根据试卷id查询问题表 返回问题对象集合
     */
    @Override
    public List<SoultestQuestion> getQuestions(Long paperId) {
        QueryWrapper<SoultestQuestion> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("paper_id",paperId);
        return  soultestQuestionMapper.selectList(queryWrapper);
    }
    /**
     * 根据问题id查询问题表
     */
    @Override
    public SoultestQuestion queryQuestionById(Long questionId) {
        QueryWrapper<SoultestQuestion> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",questionId);
        return soultestQuestionMapper.selectOne(queryWrapper);
    }
    //根据当前用户Id和试卷Id查询
    @Override
    public SoultestResult getResult(Long userId, Long paperId) {
        QueryWrapper<SoultestResult> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("paper_id",paperId);
        return soultestResultMapper.selectOne(queryWrapper);
    }
    //根据当前用户查询报告表中类别最高的记录
    @Override
    public SoultestResult getBigPaperId(Long userId) {
        QueryWrapper<SoultestResult> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.orderByDesc("paper_id");
        queryWrapper.last("limit 1");
        return soultestResultMapper.selectOne(queryWrapper);
    }
    //根据当前用户查询报告表中类别最高的记录
    @Override
    public List<SoultestOption> queryOptionListByQuestionId(Long questionId) {
        return soultestOptionMapper.getOptions(questionId);
    }


    /**
     * 根据选项id查询选项表的分数
     */
    @Override
    public SoultestOption querySoulOptionById(Integer optionId) {
       return soultestOptionMapper.selectById(optionId);
    }

    /**
     * 根据前端提供的报告id查询报告内容
     */
    @Override
    public SoultestResult getById(Long resultId) {
        return soultestResultMapper.selectById(resultId);
    }
    /**
     *根据鉴定的类型查询描述的表 得到描述的信息
     */
    @Override
    public SoultestConclusion getConclusion(Long type) {
        QueryWrapper<SoultestConclusion> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("type",type);
        return soultestConclusionMapper.selectOne(queryWrapper);
    }
    /**
     *根据鉴定的类型查询相似类型的用户 得到相似用户的集合
     */
    @Override
    public List<UserInfo> getSimilarYou(Long type,Long resultId,Integer paperId,String gender) {
        //这里使用了自定义语句的语法
        return userInfoMapper.getSimilarYou(type,resultId,paperId,gender);
    }
    /**
     *向报告表插入一条记录
     */
    @Override
    public String saveReport(SoultestResult soultestResult) {
        int result = soultestResultMapper.insert(soultestResult);
        //非空判断
        if(!ObjectUtils.isEmpty(result)){
            return String.valueOf(result);
        }
        //如果没查到对象 那么就返回null
        return null;
    }
    /**
     *保存报告记录或更新记录
     */
    @Override
    public String saveOrUpdate(SoultestResult soultestResult) {
        //创建条件
        QueryWrapper<SoultestResult> queryWrapper = new QueryWrapper<>();
        //设置查询条件为试卷id
        queryWrapper.eq("paper_id",soultestResult.getPaperId());
        queryWrapper.eq("user_id",soultestResult.getUserId());
        //根据试卷id查询数据库是否存在记录
        //根据试卷id查询数据库是否存在记录
        SoultestResult mySoulResult = soultestResultMapper.selectOne(queryWrapper);
        //设置一个变量 用于存储报告id
        Integer resultId;
        //数据库没查询到就执行插入
        if(ObjectUtils.isEmpty(mySoulResult)){
            //将数据库插入后返回的自增主键设置到变量中
            soultestResultMapper.insert(soultestResult);
            resultId=soultestResult.getId();
        }
        //如果有记录 那么就更新数据 更新条件为试卷Id
        else {
            //设置更新Id
            soultestResult.setId(mySoulResult.getId());
            //设置更新条件
            soultestResultMapper.updateById(soultestResult);
            resultId=mySoulResult.getId();
        }
        return resultId.toString();
    }


}
